package com.yuyou.hxyy.gateway.controller;

import com.yuyou.hxyy.service.api.bean.LoginRedisObj;
import com.yuyou.hxyy.service.api.bean.PageParams;
import com.yuyou.hxyy.service.api.bean.Pagination;
import com.yuyou.hxyy.service.api.bean.ReturnResult;
import com.yuyou.hxyy.service.api.entity.WordRepo;
import com.yuyou.hxyy.service.api.security.SecurityCons;
import com.yuyou.hxyy.service.api.service.IWordRepoService;
import com.yuyou.hxyy.service.api.vo.resp.UserStatisticsVo;
import com.yuyou.hxyy.service.api.vo.resp.WordRepoMapVo;
import com.yuyou.hxyy.service.api.vo.resp.WordRepoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value="WordRepoController",description="词典接口")
@Validated
@RestController
@RequestMapping(value="/api/wordRepo")
public class WordRepoController extends BaseController{

    @Autowired
    private IWordRepoService wordRepoService;

    @ApiOperation("词典接口分页查询")
    @RequestMapping(value ="/findWordRepoPage",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<WordRepoVo> findWordRepoPage(@Validated @ModelAttribute PageParams pageParams,
                                 @ApiParam(value = "类别")@RequestParam(value = "category" , required = false)Integer category,
                                 @ApiParam(value = "词库名称")@RequestParam(value = "name" , required = false) String name){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<WordRepoVo> queryPgn = new Pagination<>();
        queryPgn.setPageParams(pageParams);
        if(category != null){
            conditions.put("category", category);
        }
        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim().replace("%", "\\%").replace("_", "\\_"));
        }
        queryPgn.setConditions(conditions);
        int total = wordRepoService.findWordRepoCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<WordRepoVo> list = wordRepoService.findWordRepoPage(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }

    /*@ApiOperation("词典下拉数据")
    @PreAuthorize(SecurityCons.admin)
    @RequestMapping(value ="/findAllWordRepo",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<WordRepoVo> findAllWordRepo(@Validated @ModelAttribute PageParams pageParams,
                                 @ApiParam(value = "类别")@RequestParam(value = "category" , required = false)Integer category,
                                 @ApiParam(value = "词库名称")@RequestParam(value = "name" , required = false) String name){
        Map<String, Object> conditions = new HashMap<>();
        if(category != null){
            conditions.put("category", category);
        }
        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim());
        }
        List<WordRepoVo> list = wordRepoService.findAllWordRepo(conditions);
        return ReturnResult.success(list);
    }*/

    @ApiOperation("通过ID获取词典数据")
    @RequestMapping(value ="/getWordRepoById",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<WordRepo> getWordRepoById(@ApiParam(value = "id")@NotNull(message = "ID不能为空")@RequestParam(value = "id")Long id){
        WordRepo wr = wordRepoService.selectByPrimaryKey(id);
        return ReturnResult.success(wr);
    }

    @ApiOperation("插入词典")
    @PreAuthorize(SecurityCons.admin)
    @RequestMapping(value ="/addWordRepo",method = {RequestMethod.POST})
    public ReturnResult<Void> addWordRepo(@ApiParam(value = "类别")@NotNull(message = "类别不能为空")@RequestParam(value = "category")Integer category,
                                          @ApiParam(value = "词库名称")@NotBlank(message = "词库名称不能为空")@RequestParam(value = "name") String name,
                                          @ApiParam(value = "序号")@RequestParam(value = "sort" , required = false) Integer sort,
                                          HttpServletRequest request){
        WordRepo wr = wordRepoService.findByNameCategory(name,category);
        if(wr != null){//该类别已存在相同名称的词典
            return ReturnResult.fail("该类别已存在相同名称的词典");
        }
        LoginRedisObj user = this.getUserObj(request);
        wordRepoService.insertWordRepo(name , category , user.getUserId() , sort);
        return ReturnResult.success("OK");
    }

    @ApiOperation("更新词典")
    @PreAuthorize(SecurityCons.admin)
    @RequestMapping(value ="/updateWordRepo",method = {RequestMethod.POST})
    public ReturnResult<Void> updateWordRepo(@ApiParam(value = "id")@NotNull(message = "id不能为空")@RequestParam(value = "id")Long id,
                                             @ApiParam(value = "词库名称")@NotBlank(message = "词库名称不能为空")@RequestParam(value = "name") String name,
                                             @ApiParam(value = "序号")@RequestParam(value = "sort" , required = false) Integer sort,
                                          HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        wordRepoService.updateWordRepo(name , id , user.getUserId() , sort);
        return ReturnResult.success("OK");
    }

    @ApiOperation("批量删除词典")
    @PreAuthorize(SecurityCons.admin)
    @RequestMapping(value ="/delWordRepo",method = {RequestMethod.POST})
    public ReturnResult<Void> delWordRepo(@ApiParam(value = "词典id，多个用逗号隔开")@NotBlank(message = "id不能为空")@RequestParam(value = "ids") String ids){

        wordRepoService.deleteByIds(ids);
        return ReturnResult.success("OK");
    }



    @ApiOperation("前台获取词典列表")
    @RequestMapping(value ="/getWordRepoList",method = {RequestMethod.POST,RequestMethod.GET})
    public ReturnResult<WordRepoMapVo> getWordRepoList(HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        List<WordRepoMapVo> list = wordRepoService.getWordRepoList(user.getUserId());

        return  ReturnResult.success(list);
    }

    @ApiOperation("获取用户学习统计数据")
    @RequestMapping(value ="/getUserStatistics",method = {RequestMethod.POST,RequestMethod.GET})
    public ReturnResult<UserStatisticsVo> getUserStatistics(HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        UserStatisticsVo userStatisticsVo = wordRepoService.getUserStatistics(user.getUserId());
        return  ReturnResult.success(userStatisticsVo);
    }
}
